#!/usr/bin/python3
# _*_ coding:utf-8 _*_
# __author__ = '__Csm__'

from django.urls import reverse, resolve, reverse_lazy
from django.contrib import messages
from django.http import JsonResponse
from django.core.exceptions import PermissionDenied
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.decorators.http import require_http_methods
from django.views.generic import CreateView, ListView, DetailView

from zanhu.helpers import ajax_required
from zanhu.qa.models import Question, Answer
from zanhu.qa.forms import QuestionForm
from zanhu.notifications.views import notification_handler


class QuestionListView(LoginRequiredMixin, ListView):
    """所有问题页"""
    model = Question
    paginate_by = 10
    context_object_name = "questions"
    template_name = "qa/question_list.html"

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super(QuestionListView, self).get_context_data()
        context["popular_tags"] = Question.objects.get_counted_tags()
        context["active"] = "all"
        return context


class AnsweredQuestionListView(QuestionListView):
    """已有采纳答案的问题"""

    def get_queryset(self):
        return Question.objects.get_answered()

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super(AnsweredQuestionListView, self).get_context_data()
        context["active"] = "answered"
        return context


class UnansweredQuestionListView(QuestionListView):
    """没有采纳答案的问题"""

    def get_queryset(self):
        return Question.objects.get_unanswered()

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super(UnansweredQuestionListView, self).get_context_data()
        context["active"] = "unanswered"
        return context


class CreateQuestionListView(LoginRequiredMixin, CreateView):
    """用户提问"""
    form_class = QuestionForm
    template_name = 'qa/question_form.html'
    message = "问题已提交"

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(CreateQuestionListView, self).form_valid(form)

    def get_success_url(self):
        messages.success(self.request, self.message)
        return reverse_lazy("qa:unanswered_q")


class QuestionDetailView(LoginRequiredMixin, DetailView):
    """问题详情页"""
    model = Question
    context_object_name = 'question'
    template_name = 'qa/question_detail.html'


class CreateAnswerView(LoginRequiredMixin, CreateView):
    """回答问题"""
    model = Answer
    fields = ['content', ]
    message = "您的回答已经提交"
    template_name = "qa/answer_form.html"

    def form_valid(self, form):
        form.instance.user = self.request.user
        form.instance.question_id = self.kwargs['question_id']
        return super(CreateAnswerView, self).form_valid(form)

    def get_success_url(self):
        messages.success(self.request, self.message)
        return reverse_lazy('qa:question_detail', kwargs = {'pk':self.kwargs['question_id']})


@login_required
@ajax_required
@require_http_methods(["POST"])
def questions_vote(request):
    """给问题投票 AJAX POST 请求"""
    question_id = request.POST["question"] #当前问题id
    value = True if request.POST["value"] == "U" else False  #  U表示攒，D表示踩
    question = Question.objects.get(pk = question_id)# 当前问题
    users = question.votes.values_list('user', flat=True) # 当前问题的所有投票用户

    #  把下方三种if合并起来。分为两种情况，删除和创建更新
    if request.user.pk in users and question.votes.get(user=request.user).value == value:
        question.votes.get(user=request.user).delete()
    else:
        question.votes.update_or_create(user=request.user, defaults={"value": value})

    """
    # 用户首次操作，点赞/踩
    if request.user.pk not in users:
        question.votes.update_or_create(user=request.user, defaults={"value": value})

    # 用户已经赞过，要取消赞/踩一下
    elif question.votes.get(user = request.user).value:
        if value:
            question.votes.get(user = request.user).delete()
        else:
            question.votes.update_or_create(user=request.user, defaults={"value": value})

    # 用户已经踩过，要取消踩/赞一下
    else:
        if not value:
            question.votes.get(user = question.user).delete()
        else:
            question.votes.update_or_create(user=request.user, defaults={"value": value})
    """
    return JsonResponse({"votes":question.total_votes()})


@login_required
@ajax_required
@require_http_methods(["POST"])
def answer_vote(request):
    """给回答投票 AJAX POST 请求"""
    answer_id = request.POST["answer"] #当前回答id
    value = True if request.POST["value"] == "U" else False  #  U表示攒，D表示踩
    answer = Answer.objects.get(pk=answer_id)# 当前回答
    users = answer.votes.values_list('user', flat=True) # 当前回答的所有投票用户

    #  把下方三种if合并起来。分为两种情况，删除和创建更新
    if request.user.pk in users and answer.votes.get(user=request.user).value == value:
        answer.votes.get(user=request.user).delete()
    else:
        answer.votes.update_or_create(user=request.user, defaults={"value": value})

    return JsonResponse({"votes": answer.total_votes()})


@login_required
@ajax_required
@require_http_methods(["POST"])
def accept_answer(request):
    # 采纳答案
    answer_id = request.POST["answer"]
    answer = Answer.objects.get(pk=answer_id)
    if answer.question.user.username != request.user.username:
        return PermissionDenied
    answer.accept_answer()
    # 通知回答者
    notification_handler(request.user, answer.user, 'W', answer)
    return JsonResponse({"status": "true"})
